<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\UserExtend;
use App\Models\UserSign;
use App\Services\BalanceService;
use Illuminate\Http\Request;

class UserSignController extends BaseController
{

    public $model = UserSign::class;

    public function index(Request $request) {
        $user = $this->getUser();
        $user_id = $user->id;
        $info = UserSign::where('user_id', $user_id)->orderBy('id', 'desc')->first();
        $sign_list = [];
        $cur_date = date('Y-m-d');
        $cur_day = 1;
        $extend = UserExtend::where('user_id', $user_id)->first();
        if ($last_sign_date = $extend->last_sign_date) {
            if ($last_sign_date == $cur_date || $last_sign_date == date('Y-m-d', strtotime('-1 day', strtotime($cur_date)))) {
                $series_day = $extend->series_day;
            } else {
                $series_day = 0;
            }
        } else {
            $series_day = 0;
        }

        $last_week_count = 0;
        if ($info) {
            $diff_day = diffBetweenTwoDays($info->date, $cur_date);
            if ( ($diff_day + $info->day) > 7 && $series_day == 0 ) {
                $cur_day = 1;
            } else {
                $cur_day = ($diff_day + $info->day) % 7;
                if ($cur_day == 0)
                    $cur_day = 7;
            }


            $arr_date = [];
            for ($j = $cur_day; $j < $cur_day + 7; $j++) {
                $arr_date[] = date('Y-m-d', strtotime($cur_date) - 24 * 3600 * $j);
            }
            $last_week_count = UserSign::where('user_id', $user_id)->whereIn('date', $arr_date)->count();


            $arr = [];
            for ($i = 0; $i <= ($cur_day-1); $i++) {
                $arr[] = date('Y-m-d', strtotime($cur_date) - 24 * 3600 * $i);
            }
            $sign_list = UserSign::where('user_id', $user_id)->whereIn('date', $arr)->pluck('day')->all();
        }
        $balance = $user->balance;
        $task = BalanceService::is_finish($user_id);

        $point_list = [5,10,5,10,5,10,15];
        if ($last_week_count == 7) {
            $point_list = [15,15,15,15,15,15,15];
        }

        return $this->success(compact('sign_list', 'series_day', 'cur_day', 'balance', 'task', 'point_list'));
    }

    public function store(Request $request)
    {
        $cur_date = date('Y-m-d');
        $user_id = $this->getUserId();
        $info = UserSign::where('user_id', $user_id)->where('date', $cur_date)->first();
        if ($info)
            return $this->failed('请勿重复签到');

        $cur_day = 1;

        $info = UserSign::where('user_id', $user_id)->orderBy('id', 'desc')->first();
        if ($info) {
            $diff_day = diffBetweenTwoDays($info->date, $cur_date);
            if ( ($diff_day + $info->day) > 7) {
                $cur_day = 1;
            } else {
                $cur_day = ($diff_day + $info->day) % 7;
                if ($cur_day == 0)
                    $cur_day = 7;
            }

        }
        UserSign::create([
            'user_id' => $user_id,
            'date' => $cur_date,
            'day' => $cur_day
        ]);
        $extend = UserExtend::where('user_id', $user_id)->first();
        if ($extend) {
            if ($last_sign_date = $extend->last_sign_date) {
                if ($last_sign_date == $cur_date || $last_sign_date == date('Y-m-d', strtotime('-1 day', strtotime($cur_date)))) {
                    $extend->series_day += 1;
                } else {
                    $extend->series_day = 1;
                }
            } else {
                $extend->series_day = 1;
            }
            $extend->last_sign_date = $cur_date;
            $extend->save();
        }

        $arr_date = [];
        for ($j = $cur_day; $j < $cur_day + 7; $j++) {
            $arr_date[] = date('Y-m-d', strtotime($cur_date) - 24 * 3600 * $j);
        }
        $last_week_count = UserSign::where('user_id', $user_id)->whereIn('date', $arr_date)->count();

        BalanceService::sign_in($user_id, $cur_day, $last_week_count);

        return $this->success(['series_day' => $extend->series_day]);
    }
}

